<template>
  <div>
    <a-modal v-model="visible" title="修改信息" @ok="PutPortrait(index)">
      <div>
        <Upload @on-change="Portrait" v-if="index == 1" />
        <Userchange v-else @fromdata="Fromdata" />
      </div>
    </a-modal>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
import Upload from "../components/Upload.vue";
import Userchange from "../components/Userchange.vue";
export default {
  data() {
    return {
      visible: false,
      imgurl: "",
      index: "",
      username: "",
      gender: "",
    };
  },
  components: {
    Upload,
    Userchange,
  },
  methods: {
    ...mapMutations(["upuserinfo"]),
    showModal(index) {
      this.index = index;
      this.visible = true;
    },
    handleOk(e) {
      console.log(e);
      this.visible = false;
    },
    Portrait(val) {
      this.imgurl = val;
    },
    async PutPortrait(index) {
      let res = null;
      if (index == 1) {
        res = await this.$uploading.portrait(this.imgurl); //头像上传
        let upuser = await this.$Api.personal(); //重新获取用户详情
        if (res.data.ret == 200) {
          this.upuserinfo(upuser.data.data);
          this.visible = false;
        }
      } else {
        let parmas = {
          username:this.username,
          gender: this.gender,
          avatarUrl:"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
        };
        console.log(parmas);
        res = await this.$Api.CurrentUser(parmas); //修改用户信息
        if (res.data.ret==200) {
           this.$router.go(0)
        }

      }
    },
    Fromdata(val) {
      this.username = val.username;
      this.gender = val.gender;
    },
  },
};
</script>
